package com.wecash.jxl;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DK on 17/7/9 21:16.
 * doc:多语言工具类
 */
public class MutilLanguageUtil {
    public static void main(String[] args) {
        String xlsFile = "/Users/dk/Desktop/wecash/DifferentTranslations_2004.xls";
        File file = new File(xlsFile);
       // read(file);


        File targetFile = new File("/Users/dk/Desktop/wecash/demo.xls");
        List<String> keys = new ArrayList();
        keys.add("你好");
        keys.add("我们");

        Map<String,String> yinniMap = new HashMap<String, String>();
        yinniMap.put("你好","nihao");
        yinniMap.put("我们","women");

        Map<String,String> enMap = new HashMap<String, String>();
        enMap.put("你好","hello");
        enMap.put("我们","we");

        write2file(targetFile,keys,yinniMap,enMap);
    }


    private static void write2file(File file, List<String> keys, Map<String,String> yinniMap,Map<String,String> enMap){
        String [] str = {"中文","印尼文","英语"};//标题内容
        try {
            if(!file.exists()){//判断文件是否存在
                file.createNewFile();//创建新的文件
            }
            //创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            //创建sheet对象
            WritableSheet sheet = workbook.createSheet("sheet1", 0);
            //行对象
            Label label = null;

            //添加标题
            for (int i = 0; i < str.length; i++) {
                /**
                 * Label(i,j,String);
                 * i:列
                 * j:行
                 * String:表示内容
                 */
                label = new Label(i, 0, str[i]);
                sheet.addCell(label);
            }

            //添加数据
            for (int i = 1; i <= keys.size(); i++) {
                String chineseKey = keys.get(i-1);
                label = new Label(0, i, chineseKey);
                sheet.addCell(label);
                label = new Label(1, i, yinniMap.get(chineseKey));
                sheet.addCell(label);
                label = new Label(2, i, enMap.get(chineseKey));
                sheet.addCell(label);
            }
            workbook.write();//把表格信息写入文件
            workbook.close();//关闭
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("表格生成！");
    }



    private static void read(File file){
        try{
            if(file.exists()){//判断文件是否存在
                //创建工作簿
                Workbook workbook = Workbook.getWorkbook(file);
                //获得第一个工作表sheet1
                Sheet sheet = workbook.getSheet(0);

                //获得数据
                for (int i = 0; i < sheet.getRows(); i++) {//sheet.getRows():获得表格文件行数
                    for (int j = 0; j < sheet.getColumns(); j++) {//sheet.getColumns():获得表格文件列数
                        Cell cell = sheet.getCell(j,i);
                        System.out.print(cell.getContents()+" ");
                    }
                    System.out.println("");
                }
                workbook.close();//关闭
            }else{
                System.out.println("文件不存在");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
